const http = 'http://192.168.101.117:8084/'
//const http = 'http://www.tp6.com:8088/api/'
//真机 本地调试的第二域名 由电脑网络所连接的路由器有关 手机也要连接到该路由器上
//const http = 'http://192.168.101.117:8084/'
//const http = 'http://www.tp6.com/api/'
export default {
	show: show, //弹出框
	get: get,
	checkGet: checkGet,
	post: post,
	checkPost: checkPost,
	checkPhone: checkPhone,
	upload: upload,
	richImg: richImg,
	back: back,
	formatDate:formatDate
}
/*
	get请求
*/
function get(url, successCallback) {
	uni.request({
		url: http + url,
		header: {
//			"Authori-zation": "Bearer " + uni.getStorageSync('token')
			'content-type':'application/json',
		},
		dataType:"json",
		method: 'GET',
		success: (res) => {
			console.log(res)
			if (res.statusCode == 200) {
				successCallback(res.data)
			} else {
				show(res.data.msg)
			}
		},
		fail:(res) =>{
			console.log(res)
		}
	})
}
/*
验证get请求
*/
function checkGet(url, successCallback) {
	const headers = {};
	let token = uni.getStorageSync('token')
	if (!token) {
		uni.navigateTo({
			url: '/pages/login/login/login'
		})
		return show('未登录')
	} else {
		headers["Authori-zation"] = "Bearer " + token;
		uni.request({
			url: http + url,
			header: headers,
			success: (res) => {
				if (res.data.status == 200) {
					successCallback(res.data)
				} else if ([410000, 410001, 410002].indexOf(res.data.status) !== -1) {
					show(res.data.msg)
					uni.navigateTo({
						url: '/pages/login/login/login?judge=false'
					})
				} else {
					show(res.data.msg)
				}
			}
		})

	}
}
/*
post 请求
*/
function post(url, data, successCallback) {
	 const headers = {};
	// headers["Authori-zation"] = "Bearer " + uni.getStorageSync('token');
//	headers["content-type"] = "application/x-www-form-urlencoded";
    headers["content-type"] = "application/json";
	uni.request({
		url: http + url,
		header: headers,
		method: 'POST',
		withCredentials:true,
		data: data,
		success: (res) => {
			console.log(res)
			if (res.statusCode == 200) {
				successCallback(res.data)
			} else {
				show(res.data.msg)
			}
		},
		fail:(res) =>{
			console.log(res)
		}
	})
}
/*
验证post请求
*/
function checkPost(url, data, successCallback) {
	const headers = {};
	headers["Authori-zation"] = "Bearer " + uni.getStorageSync('token');
	if (!headers) {
		uni.navigateTo({
			url: '/pages/login/login/login.vue'
		})
		return show('未登录')
	} else {
		uni.request({
			url: http + url,
			header: headers,
			data: data,
			method: 'POST',
			success: (res) => {
				console.log([410000, 410001, 410002].indexOf(res.data.status))
				if (res.data.status == 200) {
					successCallback(res.data)
				} else if ([410000, 410001, 410002].indexOf(res.data.status) !== -1) {
					uni.navigateTo({
						url: '/pages/login/login/login?judge=false'
					})
				} else {
					show(res.data.msg)
				}
			}
		})

	}
}

function show(text) { //弹出框
	uni.showToast({
		title: text,
		icon: 'none'
	})
	setTimeout(res => {
		uni.hideToast()
	}, 800)
}

// 手机号验证
function checkPhone(phone) {
	let myreg = /^[1][3,4,5,7,8,9][0-9]{9}$/;
	if (myreg.test(phone)) {
		return true
	} else {
		show('手机号有误')
	}
}
//上传图片
function upload(successCallback) {
	const headers = {};
	headers["Authori-zation"] = "Bearer " + uni.getStorageSync('token');
	uni.chooseImage({
		count: 1,
		success: (res) => {
			uni.uploadFile({
				url: http + 'upload/image',
				header: headers,
				filePath: res.tempFilePaths[0],
				name: 'file',
				success: (res) => {
					let box = JSON.parse(res.data)
					successCallback(box)
				}

			})
		}
	})
}
//富文本图片增加格式
function richImg(richtext) {
	let rich = richtext
	const regex = new RegExp('<img', 'gi');
	const txtex = new RegExp('<span', 'gi');
	const jpgtex = new RegExp('.jpg', 'gi');
	const pngtex = new RegExp('.png', 'gi');
	rich = rich.replace(regex, `<img style="max-width: 100%;"`);
	rich = rich.replace(txtex,
		`<span style="font-size:14px;max-width:100%;line-height:1.5;box-sizing: border-box;"`)
	rich = rich.replace(jpgtex, '.jpg!yasuo')
	rich = rich.replace(pngtex, '.png!yasuo')
	// rich=rich.replace(ptex,`<p style="text-align: center;"`)
	return rich

}
//完成后返回上一层
function back() {
	setTimeout(res => {
		uni.navigateBack({
			delta: 1
		})
	}, 500)
}

function formatDate(time) {
    let date = new Date(parseInt(time)*1000);
    
    let YY = date.getFullYear();
    let MM = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
    let DD = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
    let hh = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
    let mm = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
    let ss = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();

	// 这里修改返回时间的格式
    return YY + "-" + MM + "-" + DD + " " + hh + ":" + mm + ":" + ss;
}